package com.tcp;

import java.io.DataInputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPServerTC {

    public static void main(String[] args) {
        System.out.println("======服务端启动======");
        // 创建服务端对象,要实现多个客户端向服务器发送数据，需要多线程
        try(ServerSocket socketServer = new ServerSocket(9999)) {
            Socket socket = socketServer.accept();
            DataInputStream inputStream = new DataInputStream(socket.getInputStream());
            while (true) {
                System.out.println(inputStream.readUTF());
                System.out.println("收到来自" + socket.getInetAddress().getHostAddress() + "的数据包，端口号：" + socket.getPort());
                System.out.println("===========================================");
            }

        } catch (Exception e){
            System.out.println("数据接收失败！");
            e.printStackTrace();
        }
    }
}
